package com.gzdh.tang.ali.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.response.AlipayTradePayResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzdh.tang.config.ali.AliPayUtil;
import com.gzdh.tang.modu.entity.Addr;
import com.gzdh.tang.modu.entity.AliCardAgreement;
import com.gzdh.tang.modu.service.AliCardAgreementService;
import com.gzdh.tang.modu.service.TangAddrService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.math3.analysis.function.Add;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class AliAgreementNoJob {

    @Autowired
    AliCardAgreementService aliCardAgreementService;


    @Autowired
    TangAddrService addrService;

    @XxlJob("AliAgreementNoJobHandler")
    public void AliAgreementNoJobHandler() throws Exception {
        List<Addr> addrList = addrService.list();
        List<AliCardAgreement> aliCardAgreementList = aliCardAgreementService.list(new QueryWrapper<AliCardAgreement>().lambda().eq(AliCardAgreement::getState, 0));
        for (AliCardAgreement goo : aliCardAgreementList) {
            Addr addr = addrList.stream().filter(addr1 -> addr1.getAddrNo() == goo.getAddr()).findFirst().orElse(null);
            if (addr == null) {
                return;
            }
            sendAl(goo.getAliAgreementNo(), goo.getPrice(), addr.getAliAppAuthToken(), goo.getOrders());
            goo.setState(1);
            aliCardAgreementService.updateById(goo);
        }

    }

    private void sendAl(String agreementNo, String price, String token, String order) {
        try {
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                    AliPayUtil.getAppid(),
                    AliPayUtil.getPrivateKey(), "json", "GBK", AliPayUtil.getPublicKey(), "RSA2");
            AlipayTradePayRequest request = new AlipayTradePayRequest();
            JSONObject bizContent = new JSONObject();
            bizContent.put("out_trade_no", order);
            bizContent.put("total_amount", price);
            bizContent.put("subject", "餐卡支付");
            bizContent.put("product_code", "GENERAL_WITHHOLDING");

//        代扣
            JSONObject agreementParams = new JSONObject();
            agreementParams.put("agreement_no", agreementNo);
            bizContent.put("agreement_params", agreementParams);

// 业务信息
            JSONObject businessParams = new JSONObject();
            businessParams.put("assignJointAccountId", "2088420029171855");
            businessParams.put("enterprisePayAmount", price);
            bizContent.put("business_params", businessParams);

// 扩展信息
            JSONObject extendParams = new JSONObject();
            extendParams.put("pdSubBizScene", "enterprisePay");
            bizContent.put("extend_params", extendParams);

// 返回参数选项
            JSONArray queryOptions = new JSONArray();
            queryOptions.add("enterprise_pay_info");
            bizContent.put("query_options", queryOptions);
            request.setBizContent(bizContent.toString());
            AlipayTradePayResponse response = alipayClient.execute(request, null, token);
            System.out.println(response.toString());
            if (response.isSuccess()) {
                System.out.println("调用成功");
            } else {
                System.out.println("调用失败");
                log.error(order + "IC支付失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
